package com.xnyzc.lhy.mis.service.impl.banner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.BannerException;
import com.xnyzc.lhy.common.penum.banner.BannerErrorCode;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.banner.HomePageBanner;
import com.xnyzc.lhy.mis.entity.param.banner.BannerChangeStatusParam;
import com.xnyzc.lhy.mis.entity.param.banner.BannerSaveParam;
import com.xnyzc.lhy.mis.entity.param.banner.BannerSearchParam;
import com.xnyzc.lhy.mis.entity.result.banner.BannerSearchResult;
import com.xnyzc.lhy.mis.mapper.banner.HomePageBannerMapper;
import com.xnyzc.lhy.mis.service.banner.IHomePageBannerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 首页轮播图 服务实现类
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-28
 */
@Service
public class HomePageBannerServiceImpl extends ServiceImpl<HomePageBannerMapper, HomePageBanner> implements IHomePageBannerService {

    @Autowired
    HomePageBannerMapper homePageBannerMapper;
    /**
     * banner列表查询
     *
     * @return
     */
    @Override
    public List<BannerSearchResult> queryBannerList(BannerSearchParam bannerSearchParam) {
        if(bannerSearchParam == null)
        {
            throw new BannerException(BannerErrorCode.paramEmptyError);
        }

        //查询banner图
        QueryWrapper<HomePageBanner> homePageBannerQueryWrapper = new QueryWrapper<HomePageBanner>();

        //状态。0=停用，1=启用
        if(StringUtils.isNotEmpty(bannerSearchParam.getBannerStatus())) {
            homePageBannerQueryWrapper.eq(HomePageBanner.BANNER_STATUS, bannerSearchParam.getBannerStatus());
        }

        //标题
        if(StringUtils.isNotEmpty(bannerSearchParam.getTitle())){
            homePageBannerQueryWrapper.like(HomePageBanner.TITLE, bannerSearchParam.getTitle());
        }

        //正常记录 =0
        homePageBannerQueryWrapper.eq(HomePageBanner.DELETED, 0);

        //排序：1.启用-停用 2.排序值从小到大
        homePageBannerQueryWrapper.orderByDesc(HomePageBanner.BANNER_STATUS).orderByAsc(HomePageBanner.SEQUENCE);

        //查询数据
        List<HomePageBanner> homePageBannerList = homePageBannerMapper.selectList(homePageBannerQueryWrapper);

        return this.convertBannerSearchResult(homePageBannerList);
    }

    /**
     * banner启动停用
     *
     * @return
     */
    @Override
    public void changeBannerStatus(BannerChangeStatusParam bannerChangeStatusParam) {
        if(bannerChangeStatusParam == null
                || StringUtils.isEmpty(bannerChangeStatusParam.getBannerId())
                || StringUtils.isEmpty(bannerChangeStatusParam.getBannerStatus()))
        {
            throw new BannerException(BannerErrorCode.paramEmptyError);
        }

        //查询banner图
        HomePageBanner homePageBanner = homePageBannerMapper.selectById(bannerChangeStatusParam.getBannerId());
        if("0".equals(bannerChangeStatusParam.getBannerStatus())) {
            homePageBanner.setBannerStatus(false);
        }
        else{
            homePageBanner.setBannerStatus(true);
        }
        homePageBannerMapper.updateById(homePageBanner);
    }

    /**
     * banner新增编辑
     *
     * @return
     */
    @Override
    public void saveBanner(BannerSaveParam bannerSaveParam) {
        if(bannerSaveParam == null
                || StringUtils.isEmpty(bannerSaveParam.getBannerStatus())
                || StringUtils.isEmpty(bannerSaveParam.getPictureId())
                || StringUtils.isEmpty(bannerSaveParam.getTitle())
                || StringUtils.isEmpty(bannerSaveParam.getHref())
                || StringUtils.isEmpty(bannerSaveParam.getSequence()))
        {
            throw new BannerException(BannerErrorCode.paramEmptyError);
        }
        HomePageBanner homePageBanner = null;

        if(StringUtils.isEmpty(bannerSaveParam.getBannerId())){
            //
            homePageBanner = this.createHomePageBanner(homePageBanner, bannerSaveParam);
            Long bannerId = IDUtil.nextId();
            homePageBanner.setBannerId(bannerId);

            //获取活动定义数据
            Date createDate = new Date();

            homePageBanner.setCreateTime(createDate);
            homePageBanner.setCreateUser(TokenUtil.getTokenUserId());
            homePageBanner.setDeleted(0);
            homePageBanner.setUpdateTime(createDate);

            homePageBannerMapper.insert(homePageBanner);
        }
        else {
            homePageBanner = homePageBannerMapper.selectById(bannerSaveParam.getBannerId());
            if(homePageBanner == null){
                throw new BannerException(BannerErrorCode.bannerNoneError);
            }else {
                homePageBanner = this.createHomePageBanner(homePageBanner, bannerSaveParam);
                homePageBannerMapper.updateById(homePageBanner);
            }
        }
    }

    /**
     * 创建banner保存数据
     * @param homePageBanner
     * @param bannerSaveParam
     * @return
     */
    private HomePageBanner createHomePageBanner(HomePageBanner homePageBanner, BannerSaveParam bannerSaveParam){
        if(homePageBanner == null){
            homePageBanner = new HomePageBanner();
        }

        //状态。0=停用，1=启用
        if("0".equals(bannerSaveParam.getBannerStatus())) {
            homePageBanner.setBannerStatus(false);
        }
        else{
            homePageBanner.setBannerStatus(true);
        }

        //图片ID
        homePageBanner.setPictureId(Long.parseLong(bannerSaveParam.getPictureId()));
        //标题
        homePageBanner.setTitle(bannerSaveParam.getTitle());
        //跳转地址
        homePageBanner.setHref(bannerSaveParam.getHref());
        //排序值
        homePageBanner.setSequence(Integer.parseInt(bannerSaveParam.getSequence()));

        return homePageBanner;
    }

    /**
     * 创建查询返回数据
     * @param homePageBannerList
     * @return
     */
    private List<BannerSearchResult> convertBannerSearchResult(List<HomePageBanner> homePageBannerList) {
        if(homePageBannerList !=null
                && homePageBannerList.size() >0){
            List<BannerSearchResult> bannerSearchResultList = new ArrayList<BannerSearchResult>(homePageBannerList.size());
            for (HomePageBanner homePageBanner : homePageBannerList) {
                BannerSearchResult bannerSearchResult = new BannerSearchResult();

                //ID
                bannerSearchResult.setBannerId(homePageBanner.getBannerId().toString());
                //图片id
                if(homePageBanner.getPictureId() != null) {
                    bannerSearchResult.setPictureId(homePageBanner.getPictureId().toString());
                }
                //状态。0=停用，1=启用
                bannerSearchResult.setBannerStatus(homePageBanner.getBannerStatus().toString());
                //标题
                bannerSearchResult.setTitle(homePageBanner.getTitle());
                //跳转地址
                bannerSearchResult.setHref(homePageBanner.getHref());
                //排序值
                if(homePageBanner.getSequence() != null) {
                    bannerSearchResult.setSequence(homePageBanner.getSequence().toString());
                }

                bannerSearchResultList.add(bannerSearchResult);
            }

            return bannerSearchResultList;
        }
        else{
            return null;
        }
    }
}
